

#region copyright notice
/* ====================================================================
 * The UncleGED Open Source Software License
 *
 * Copyright (c) 1999-2009 D. M. DeBacker. All rights reserved.
 * http://gatheringleaves.org/uged
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Products derived from this software may not be called "UncleGED",
 *    nor may "UncleGED" appear in their name, without prior written
 *    permission of D. M. DeBacker.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL D. M. DEBACKER BE LIABLE FOR ANY 
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;

using UncleGED;
using UncleGED.Utils;
using UncleGED.Application;

namespace UGed
{
   public static class ConfigReader
   {

      #region Config Settings
      /// <summary>
      /// Summary of SaveXmlSettings.
      /// </summary>
      /// <param name=sXmlConfig></param>
      /// <param name=ConfigSettings></param>
      /// 		
      public static void SaveProject(string projectFile, UGedProject projectData)
      {
         XmlSerializer XmlS = new XmlSerializer(typeof(UGedProject));
         FileStream fStream = new FileStream(projectFile, FileMode.Create);
         XmlS.Serialize(fStream, projectData);
         fStream.Close();
      }

      /// <summary>
      /// Summary of LoadXmlSettings.
      /// </summary>
      /// <param name=sXmlConfig></param>
      /// 		
      public static UGedProject LoadProject(string projectFile)
      {
         UGedProject configSettings;

         try
         {
            XmlSerializer XmlS = new XmlSerializer(typeof(UGedProject));
            FileStream fStream = new FileStream(projectFile, FileMode.Open);
            configSettings = (UGedProject)XmlS.Deserialize(fStream);
            fStream.Close();
            fStream = null;
            XmlS = null;
         }
         catch 
         {           
            configSettings = new UGedProject();
            SaveProject(projectFile, configSettings);
         }

         return configSettings;

      }
      #endregion

      #region  Local application
      public static void SaveSettings(string SettingsFile, LocalApplication SettingsData)
      {
         XmlSerializer XmlS = new XmlSerializer(typeof(LocalApplication));
         FileStream fStream = new FileStream(SettingsFile, FileMode.Create);
         XmlS.Serialize(fStream, SettingsData);
         fStream.Close();
      }

      /// <summary>
      /// Summary of LoadXmlSettings.
      /// </summary>
      /// <param name=sXmlConfig></param>
      /// 		
      public static LocalApplication LoadSettings(string SettingsFile)
      {
         LocalApplication configSettings;

         try
         {
            XmlSerializer XmlS = new XmlSerializer(typeof(LocalApplication));
            FileStream fStream = new FileStream(SettingsFile, FileMode.Open);
            configSettings = (LocalApplication)XmlS.Deserialize(fStream);
            fStream.Close();
            fStream = null;
            XmlS = null;
         }
         catch
         {
            configSettings = new LocalApplication();
            SaveSettings(SettingsFile, configSettings);
         }

         return configSettings;

      }

      #endregion

   }
}
